package hello

import (
	"appengine"
	"net/http"
)

func actionAdminCategories(c *appengine.Context, w http.ResponseWriter, r *http.Request) {
	categories := listAllCategories(c)	
	adminTemplates.ExecuteTemplate(w, "categories.html", categories)
}

func actionAdminAddCategory(c *appengine.Context, w http.ResponseWriter, r *http.Request) {
	saveCategory(c, &Category{Name: r.FormValue("name"), Code: r.FormValue("code")})
	http.Redirect(w, r, "categories", http.StatusFound)	
}

func actionAdminDeleteCategory(c *appengine.Context, w http.ResponseWriter, r *http.Request) {
	deleteCategory(c, parseInt(r.FormValue("id")))
	http.Redirect(w, r, "categories", http.StatusFound)	
}

func actionAdminEditCategoryForm(c *appengine.Context, w http.ResponseWriter, r *http.Request) {
	category := getCategory(c, parseInt(r.FormValue("id")))
	adminTemplates.ExecuteTemplate(w, "editCategory.html", category)
}

func actionAdminUpdateCategory(c *appengine.Context, w http.ResponseWriter, r *http.Request) {
	id := parseInt(r.FormValue("id"))
	updateCategory(c, id, &Category{Name: r.FormValue("name"), Code: r.FormValue("code")})
	if len(r.FormValue("saveAndContinue")) != 0 {
		http.Redirect(w, r, "editCategoryForm?id=" + r.FormValue("id"), http.StatusFound)
		return
	}
	http.Redirect(w, r, "categories", http.StatusFound)
}
